<?php

/*
 * EmailField    extends FormField
 */

// Restrict Unrestricted Access

defined("_dispatch") or	die("Access Denied !");

define('DOMROOT', dirname(dirname(dirname(__FILE__))));

require_once DOMROOT.'/classes/FormsAPI/FormField.php';
require_once DOMROOT.'/classes/FormsAPI/TextHTMLHandler.php';

class   EmailField   extends FormField{
    
    private $_HTMLHandler;
    private $_required  =   false;

    public function __construct($_name) {

        parent::setName($_name);
        
        $this->_HTMLHandler = new TextHTMLHandler();
        
    }
    
    public function setValue($_value) {
        
        parent::setValue($_value);
    }
    
    public function getValue() {
     
        return parent::getValue();
        
    }
    
    public  function    isRequired( ){

            
            $this->_required    =   TRUE;
    }

    public function validate(){
        
          //override
        
        $pattern ="^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$^";
        
        if($this->_required){
            
            if(!parent::getValue())
            { parent::setDefaultError("* Mandatory");
              return false;
            }
            else if(!preg_match($pattern, parent::getValue()))
            {
              parent::setDefaultError("* Invalid email");
              return false;
            }
            else
            {
              return true;
            }
        }
    }
    
    public  function    getHTMLHandler(){
        
        return $this->_HTMLHandler;
    }
    
    public function render(){
        
        return $this->getHTMLHandler()->render();
       
    }
    
}


?>
